package 动态代理;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class demo {
    public static void main(String[] args) throws Exception {
        xiaojingling xiaojingling = new xiaojingling();
        ClassLoader classLoader = xiaojingling.getClass().getClassLoader();
        Class<?>[] interfaces = xiaojingling.getClass().getInterfaces();
        Monster monster = (Monster) Proxy.newProxyInstance(classLoader, interfaces, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if ("attack".equals(method.getName())) {
                    System.out.println(proxy.getClass());
                    System.out.println("拦截!");
                    return method.invoke(xiaojingling, args);
                } else {
                    return method.invoke(xiaojingling);
                }
            }
        });
        monster.attack();
        monster.addHp();
        monster.reduceHP();
    }
}
